﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Address.Services.Interfaces;
using OOS.Core.Address;

namespace OOS.Application.Address.Services
{
    /// <summary>
    /// 地址管理
    /// </summary>
    public class AddressBookService: IAddressBookService,ITransient
    {
        private readonly ISqlSugarClient _client;

        public AddressBookService(ISqlSugarClient client)
        {
            _client = client;
        }

        /// <summary>
        /// 添加地址
        /// </summary>
        /// <param name="addressBook"></param>
        /// <returns></returns>
        public bool InsertAddressBook(AddressBook addressBook)
        {
            if (addressBook == null) { return false; }
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return false; }
            addressBook.UserId = userId;
            addressBook.CreateDate = DateTime.Now;
            addressBook.UpdateDate = DateTime.Now;
            addressBook.IsDeleted = false;
            return _client.Insertable(addressBook).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id查询地址详情(原始数据)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AddressBook SelectAddressBookById(int id)
        {
            if (id <= 0) return new AddressBook();
            return _client.Queryable<AddressBook>()
                .Where(a => a.IsDeleted == false)
                .Where(a => a.Id == id)
                .First();
        }

        /// <summary>
        /// 根据token中的用户id获取地址列表
        /// </summary>
        /// <returns></returns>
        public List<AddressBook> SelectAddressBooksByUserId()
        {
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return new List<AddressBook>(); }
            return _client.Queryable<AddressBook>()
                .Where(ab => ab.IsDeleted == false)
                .Where(ab => ab.UserId == userId)
                .OrderBy(ab => ab.IsDefault, OrderByType.Desc)
                .OrderBy(ab => ab.CreateDate, OrderByType.Desc)
                .ToList();
        }

        /// <summary>
        /// 根据id修改地址
        /// </summary>
        /// <param name="addressBook"></param>
        /// <returns></returns>
        public bool UpdateAddressBookById(AddressBook addressBook)
        {
            if (addressBook == null) return false;
            AddressBook address = this.SelectAddressBookById(addressBook.Id);
            address.Name = addressBook.Name;
            address.Phone = addressBook.Phone;
            address.ProvinceCode = addressBook.ProvinceCode;
            address.ProvinceName = addressBook.ProvinceName;
            address.CityName = addressBook.CityName;
            address.DistrictName = addressBook.DistrictName;
            address.Detail = addressBook.Detail;
            address.IsDefault = addressBook.IsDefault;
            address.IsDeleted = addressBook.IsDeleted;
            address.UpdateDate = DateTime.Now;
            return _client.Updateable(address).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id删除地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteAddressBookById(int id)
        {
            if (id <= 0) return false;
            AddressBook addressBook = this.SelectAddressBookById(id);
            addressBook.IsDeleted = true;
            return this.UpdateAddressBookById(addressBook);
        }
    }
}
